package com.Action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.InviteMemberInfo;
import com.Entity.Page.Json;
import com.Service.FamiliesService;
import com.Service.InviteMemberService;
import com.Service.UserService;

public class MessageAction extends BaseAction {

	private int flag = 0;

	@Autowired
	private InviteMemberService inviteService;
	@Autowired
	private UserService userService;
	@Autowired
	private FamiliesService familyService;

	private List<InviteMemberInfo> invitelist = new ArrayList<InviteMemberInfo>();

	public List<InviteMemberInfo> getInvitelist() {
		return invitelist;
	}

	public void setInvitelist(List<InviteMemberInfo> invitelist) {
		this.invitelist = invitelist;
	}

	// 家庭组邀请处理通知
	public void getFamily() throws UnsupportedEncodingException, ParseException {

		String userid = ServletActionContext.getRequest()
				.getParameter("userid");

		Json json = new Json();

		if (null != userid && !userid.isEmpty()) {
			invitelist = this.inviteService.getBackValidates(Integer
					.valueOf(userid));
			List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();

			if (null != invitelist && !invitelist.isEmpty()) {

				for (int i = 0; i < invitelist.size(); i++) {
					InviteMemberInfo inviteGroup = invitelist.get(i);
					flag = inviteGroup.getStatus();
					int familyid = inviteGroup.getFamilyId();
					int inviteid = inviteGroup.getInviteUserId();
					if (flag != 0) {
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("flag", flag);
						map.put("invitename", this.userService
								.getById(inviteid).getUserName());
						map.put("familyname",
								this.familyService.getById(familyid)
										.getFamilyName());
						map.put("message", inviteGroup.getMessage());

						mapList.add(map);
						inviteGroup.setMessage((short) 1);
						this.inviteService.save(inviteGroup);
					}
				}
				if (null != mapList && !mapList.isEmpty()) {
					json.setObj(mapList);
					json.setMsg("你有新消息！");
					json.setSuccess(true);
				} else {
					json.setMsg("你没有消息通知！");
					json.setSuccess(false);
				}

			} else {
				json.setMsg("你还没有发出任何邀请！");
				json.setSuccess(false);
			}
			writeJson(json);
		}
	}

}
